package com.itcanteen.medical.patient.controller;

import com.github.pagehelper.PageInfo;

import com.itcanteen.medical.common.vo.CommonResponse;
import com.itcanteen.medical.patient.entity.*;
import com.itcanteen.medical.patient.service.DoctorService;
import com.itcanteen.medical.patient.service.PatientService;
import com.itcanteen.medical.patient.service.RecordingService;
import com.itcanteen.medical.patient.service.impl.FaceMatchImpl;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/patient")
@Slf4j
public class PatientController {
    @Autowired
    PatientService patientService;

    @GetMapping("/findAll")
    public PageInfo<Patient> getAll(Patient patient) {
        List<Patient> patientList = patientService.getAll(patient);
        return new PageInfo<Patient>(patientList);
    }

    @PostMapping("/fice")
    public result fice(@RequestBody Image image){

        result s = FaceMatchImpl.faceMatch(image);
        return s;
    }


    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/redis")
    public List<Patient> findredis() {

        List<Patient> patientList = (List<Patient>) redisTemplate.boundHashOps("qwe").get("patient");

        if (patientList == null) {

            patientList = patientService.findAll();

            redisTemplate.boundHashOps("qwe").put("patient", patientList);

        }

        return patientList;
    }

    /*
     * 查询id
     * */

    @GetMapping("findByid")
    public Patient findById(Patient patient) {

        return patientService.findByid(patient);
    }

    @Autowired
    private RecordingService recordingService;

    @GetMapping("findRecording")
    public List<RegistrationRecording> findRecording() {

        List<RegistrationRecording> recordingList = (List<RegistrationRecording>) redisTemplate.boundHashOps("qwe").get("recording");

        if (recordingList == null) {

            recordingList = recordingService.findAll();

            redisTemplate.boundHashOps("asd").put("recording", recordingList);


            redisTemplate.opsForValue().set("aaa", recordingList.get(0).getDoctorId());

            String aaa = (String) redisTemplate.opsForValue().get("aaa");
            System.out.println(aaa);


        }
        return recordingList;
    }


    //患者挂号
    @PostMapping("/add")
    public CommonResponse addRecording(@RequestBody RegistrationRecording recording){
        int i=  recordingService.add(recording);
        if (i>0){
            return new CommonResponse(1,"挂号成功");
        }else {
            return new CommonResponse(0,"挂号失败");
        }
    }


    @Autowired
    DoctorService doctorService;

    @GetMapping("/findList")
    List<Map<String, Object>> findList() {
        return doctorService.findList();
    }

    /**
     * 所有医生的查询
     */
    @GetMapping("/doctorfindall")
    public List<Doctor> findByDoctorAll(){
        List<Doctor> byDoctorAll = patientService.findByDoctorAll();
        return byDoctorAll;
    }

    /*
     * 医生列表的分页加模糊
     * */
    @PostMapping("/getAllDoctor/{page}/{rows}")
    public List<Doctor> getAllDoctor(@RequestBody Doctor doctor,@PathVariable(name = "page") Integer page,@PathVariable(name = "rows") Integer rows){
        List<Doctor> allDoctor = patientService.getAllDoctor(doctor,page,rows);
        return allDoctor;
    }
    /*
     * 根据医生的id查询所有的工作时间
     * */
    @GetMapping("/findByTime/{doctorId}")
    public List<Work> findByTime(@PathVariable("doctorId") Long doctorId){
        List<Work> byTime = patientService.findByTime(doctorId);
        return byTime;
    }

    /*
     *根据id查看当前医生是否是工作时间
     * */
    @GetMapping("/findByWork/{workId}")
    public CommonResponse findByWork(@PathVariable("workId")Long workId){
        CommonResponse byWork = patientService.findByWork(workId);
        return byWork;
    }

}
